<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">系统登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
        <!-- <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" :prefix-icon="User"></el-input>
        </el-form-item> -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" :prefix-icon="Lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleLogin" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '../api/user'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456'
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}


// 添加样式
// 移除 JavaScript 样式注入，改用 style 标签

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  const valid = await loginFormRef.value.validate()
  if (!valid) return
  
  try {
    loading.value = true
    const response = await login(loginForm)
    const { success, result, message } = response! || {}
    if (success && result) {
      localStorage.setItem('userInfo', JSON.stringify(result))
      // 确保使用正确的 token 字段（根据实际响应调整）
      localStorage.setItem('token', result.id) 
      ElMessage.success('登录成功')
      router.push('/list')
    } else {
      ElMessage.error(message || '登录失败：未知原因')
    }
  } catch (error: any) {
    // 捕获网络错误和运行时异常
    const msg = error.response?.data?.message 
              || error.message 
              || '登录过程异常'
    ElMessage.error(msg)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  box-sizing: border-box;
  padding: 0;
}

.login-card {
  width: 90%;
  max-width: 420px;
  max-height: 100%;
  padding: 1.5rem;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.login-title {
  text-align: center;
  color: #303133;
  margin: 0 0 1.5rem;
  font-size: 1.5rem;
  font-weight: 500;
  flex: 0 0 auto;
}

.login-form {
  flex: 0 1 auto;
  overflow-y: auto;
  padding: 0.5rem 0;
}

.login-form :deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

.login-button {
  width: 100%;
}

@media screen and (max-width: 768px) {
  .login-container {
    align-items: stretch;
  }

  .login-card {
    width: 100%;
    height: 100%;
    max-width: 100%;
    margin: 0;
    box-shadow: none;
    border-radius: 0;
    padding: 1rem;
  }
  
  .login-title {
    font-size: 1.25rem;
    margin-bottom: 1rem;
  }

  .login-form {
    padding: 0;
  }
}
</style>
